package com.accounting.client.remote.enumeration;

import java.util.List;
import java.util.Map;

import com.accounting.client.resource.BoxResources;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

import corelib.client.CollectionUtil;
import corelib.client.delegate.Func1;
import corelib.client.linq.Linq;

public class Courier {
	
	public final static Courier EverFast   = new Courier("Ever Fast",   "EWE",    100);
    public final static Courier OneExpress = new Courier("One Express", "壹速递",   200);
    public final static Courier AuPost     = new Courier("AuPost",      "澳洲邮政", 300);
    public final static Courier AuExpress  = new Courier("AuExpress",   "澳邮中国快递",    500);
    public final static Courier Other      = new Courier("Other",       "其他或中国邮递",    999);
    
    public final static List<Courier> All = CollectionUtil.newArrayList(OneExpress, EverFast, AuExpress, AuPost, Other);
    
    public final static Map<String,Courier> NameIndex = Linq.toMap(All, new Func1<Courier,String>(){
		@Override
		public String run(Courier param) {
			return param.getName();
		}
    });
    
    public final static Map<Integer,Courier> CodeIndex = Linq.toMap(All, new Func1<Courier,Integer>(){
		@Override
		public Integer run(Courier param) {
			return param.getCode();
		}
    });
    
    private String Name;
    private String CName;
    private int    Code;
    
    private Courier(String name, String cname, int code) {
        Name  = name;
        CName = cname;
        Code  = code;
    }
    
    public String getName() {
		return Name;
	}

	public String getCName() {
		return CName;
	}

	public int getCode() {
		return Code;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null || this == null){
			return false;
		}
		
		if (!(obj instanceof Courier)){
			return false;
		}
		Courier courier = (Courier) obj;
		return Code == courier.getCode() && Name.equals(courier.getName());
	}
	
	public static Courier getInstance(int code){
		return CodeIndex.get(code) == null ? Other : CodeIndex.get(code);
	}
	
	public static SafeHtml GetImageAsHtml(int code){
		Courier courier = CodeIndex.get(code);
		if (courier == Courier.OneExpress){
			return AbstractImagePrototype.create(BoxResources.INSTANCE.oneExpressLogo()).getSafeHtml();
		}
		else if (courier == Courier.EverFast){
			return AbstractImagePrototype.create(BoxResources.INSTANCE.eweLogo()).getSafeHtml();
		}
		else if (courier == Courier.AuPost){
			return AbstractImagePrototype.create(BoxResources.INSTANCE.notAvailable()).getSafeHtml();
		}
		else if (courier == Courier.AuExpress){
			return AbstractImagePrototype.create(BoxResources.INSTANCE.auExpressLogo()).getSafeHtml();
		}
		else {
			return AbstractImagePrototype.create(BoxResources.INSTANCE.notAvailable()).getSafeHtml();
		}
    }

}
